﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using ProjectoClase3;

namespace GameProEngine
{
    public class Actor
    {
        //public Rectangle AABB;//hacer funcion
        public Vector2 position;
        public Vector2 direction;
        public float rotation, angularVelocity= 0.01f;
        public Vector2 Gravity= new Vector2(0,-1);
        public bool isEnable;
       // public Texture2D tex;
        public Animation anim;

        public Actor(Animation animo,Vector2 pos, Vector2 dir)
        {
            anim = animo;
            //tipito.tex = Content.Load<Texture2D>("tipito");
            position = pos;
            direction = dir;
            isEnable = true;
        }
        public virtual void update(int dTime)
        {
            rotation += angularVelocity;
            direction -= Gravity;
            position+= direction;
           // position -= Gravity;
            //position.Y += Gravity;
            /*Rectangle r = GetAABB();
            if (position.X + r.Width / 2 > 800 || position.X - r.Width / 2 < 0)
            {
                if ((position.X + r.Width)/2 > 800)
                {
                    position.X = 800 - (r.Width/2);
                }
                else if (position.X < 0)
                {
                    position.X = r.Width / 2;
                }
                direction.X *= -1;
            }
            if (position.Y + (r.Height / 2) > 600 || position.Y - r.Height / 2 < 0)
            {
                if (position.Y + ( r.Height)/2 > 600)
                {
                    position.Y = 600 - ( r.Height/2);
                }
                else if ((position.Y-r.Height)/2 < 0)
                {
                    position.Y = r.Height;
                }
                direction.Y *= -1;
            }*/
            anim.update(dTime);
        }
       
        public Rectangle GetAABB()
        {
            
            Vector2[] bb = GetBB();
            int Ymax=((int)bb[0].Y), Xmax = (int)bb[0].X, Ymin=(int)bb[0].Y, Xmin=(int)bb[0].X;


            for (int j = 1; j < bb.Length; j++)
            {
                if (bb[j].X > Xmax)
                {
                    Xmax = (int)bb[j].X;
                }
                else if (bb[j].X < Xmin)
                {
                    Xmin = (int)bb[j].X;
                }
                if (bb[j].Y < Ymin)
                {
                    Ymin = (int)bb[j].Y;
                }
                else if (bb[j].Y > Ymax)
                {
                    Ymax = (int)bb[j].Y;
                }               
            }




            return new Rectangle((int)Xmin, (int)Ymin, (int)(Xmax - Xmin), (int)(Ymax - Ymin));
        }
        public Vector2[] GetBB()
        {
            Vector2 vector1, vector2, vector3, vector4;
            vector1 = new Vector2(-anim.w / 2, -anim.h / 2);
            vector2 = new Vector2(anim.w / 2, -anim.h / 2);
            vector4 = new Vector2(-anim.w / 2, anim.h / 2);
            vector3 = new Vector2(anim.w / 2, anim.h / 2);

            Matrix m = Matrix.CreateRotationZ(rotation);
            vector1 = Vector2.Transform(vector1, m);
            vector2 = Vector2.Transform(vector2, m);
            vector3 = Vector2.Transform(vector3, m);
            vector4 = Vector2.Transform(vector4, m);

            vector1 += position;
            vector2 += position;
            vector3 += position;
            vector4 += position;

            return new Vector2[] { vector1, vector2, vector3, vector4 };
        }
        public void isColliding(Actor withCollisioned)
        {
           // Enable = false;
        }

    }
    
}
